import * as commonService from '../../services/common';
import {queryRoomList} from '../../services/tRoom';
import { routerRedux } from 'dva/router';
import { message } from 'antd';

export default {
  namespace: 'common',
  state: {
    loginLoading: false,
    realName: '',
    confirmLoading: false,
    newKey : '',
  },
  reducers: {
    showLoginLoading (state) {
      return {
        ...state,
        loginLoading: true,
      }
    },
    hideLoginLoading (state) {
      return {
        ...state,
        loginLoading: false,
      }
    },
    loginSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    showModal (state) {
      return { ...state, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },

  },
  effects: {
    *login({ payload },{ call, put }) {
      yield put({ type: 'showLoginLoading' })
      const { body }  = yield call(commonService.login, payload);
      yield put({ type: 'hideLoginLoading' })
      if(body.httpCode === 200){
       // yield put({ type: 'loginSuc',payload: {realName : body.data} });
        localStorage.setItem("realName",body.data);
        localStorage.setItem("permissions",JSON.stringify(body.permissions));
        yield put(routerRedux.push('/'))
      }
    },
    *logout({ payload },{ call, put }) {
      const { body }  = yield call(commonService.logout, payload);
      if(body.httpCode === 200){
          localStorage.removeItem("realName");
          localStorage.removeItem("permissions");        
        yield put(routerRedux.push('/login'))
      }
    },
    *enterAuth({payload, onComplete}, {call, put}) {
      //效验SESSION
      const { body }  = yield call(commonService.enterAuth);
      if(body.httpCode !== 200){
       yield put(routerRedux.push('/login'))
      }else{
       onComplete();
      }
    },
    *editPassword({ payload }, { call, put }) {
      const { body }  = yield call(commonService.editPassword,payload);
      if(body.httpCode === 200){
        yield put({ type: 'hideModal' })
        message.success("密码修改成功")
      }
    },
  },
  subscriptions: {
      setup({ dispatch, history }) {

      },
  },
};
